package org.leonos.utils;

import com.jfinal.core.Controller;
import com.jfinal.log.Log;

/**
 * 用于事务控制、获取分页信息的控制器
 * @author LeonXiao
 */
public class GenericController extends Controller {

    private final static Log log = Log.getLog(GenericController.class);

    /**
     * 开启事务
     */
    protected void beginTx(){
        try {
            Tx4Jfinal.beginTran();
        } catch (Exception e) {
            log.error("事务开启失败！", e);
        }
    }

    /**
     * 提交事务
     */
    protected void commitTx(){
        try {
            Tx4Jfinal.commit();
        } catch (Exception e) {
            log.error("事务开启失败！", e);
        }
    }

    /**
     * 事务回滚
     */
    protected void rollback(){
        try {
            Tx4Jfinal.rollback();
        } catch (Exception e) {
            log.error("事务回滚失败！", e);
        }
    }

    /**
     * 拿到多少页
     *
     * @return 页数
     */
    protected int getPageNumber() {
        try {
            String s = getPara("pageCurrent");
            if (Utils.isNull(s))
                return 1;
            int pa = Integer.parseInt(s);
            if (pa >= 1) {
                return pa;
            } else {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
    }

    /**
     * 拿到多少页
     * @param variate
     *              取值的变量，默认pageCurrent
     * @return 页数
     */
    protected int getPageNumber(String variate) {
        return getPageNumber(variate,1);
    }

    /**
     * 拿到多少页
     * @param variate
     *              取值的变量，默认pageCurrent
     * @param defaultvalue
     *              默认值
     * @return 页数
     */
    protected int getPageNumber(String variate, int defaultvalue) {
        try {
            if(defaultvalue <= 0) defaultvalue = 1;
            String s = getPara(variate);
            if (Utils.isNull(s))
                return defaultvalue;
            int pa = Integer.parseInt(s);
            if (pa >= 1) {
                return pa;
            } else {
                return defaultvalue;
            }
        } catch (Exception e) {
            return defaultvalue;
        }
    }

    /**
     * 一页多少条
     * @return 一页的条数
     */
    protected int getPageSize() {
        try {
            String s = getPara("pageSize");
            if (Utils.isNull(s))
                return 10;
            int pa = Integer.parseInt(s);
            if (pa >= 1) {
                return pa;
            } else {
                return 10;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 10;
        }
    }

    /**
     * 一页多少条
     * @param variate
     *              取值的变量，默认pageSize
     * @return 一页的条数
     */
    protected int getPageSize(String variate) {
        return getPageSize(variate, 10);
    }

    /**
     * 一页多少条
     * @param variate
     *              取值的变量，默认pageSize
     * @param defaultvalue
     *              默认值
     * @return 一页的条数
     */
    protected int getPageSize(String variate, int defaultvalue) {
        try {
            if (defaultvalue <= 0) defaultvalue = 10;
            String s = getPara(variate);
            if (Utils.isNull(s))
                return defaultvalue;
            int pa = Integer.parseInt(s);
            if (pa >= 1) {
                return pa;
            } else {
                return defaultvalue;
            }
        } catch (Exception e) {
            return defaultvalue;
        }
    }

    /**
     * 取得排序信息
     * @return 排序字段与升降序
     */
    protected String orderBy() {
        StringBuilder sb = new StringBuilder();
        String od = getPara("orderField");
        if(Utils.isNull(od))return null;
        sb.append(od);
        sb.append(" ");
        sb.append(getPara("orderDirection") == null ? "" : getPara("orderDirection"));
        return sb.toString();
    }

}
